import { Body, Controller, Get, Param, ParseIntPipe, Post, Req } from '@nestjs/common';
import { DeviceService } from './device.service';
import { ReqAttrs } from '../../constant';
import { ResponseUtilService } from '@app/response-util';

@Controller('device')
export class DeviceController {
	constructor(
		private readonly service:DeviceService
	){}

	@Get('list')
	async list(@Req() req) {
		const list = await this.service.deviceOfUser(req[ReqAttrs.USER_ID])
		return ResponseUtilService.successRes(list)
	}

	@Post('bind')
	async bind(@Body('id', ParseIntPipe) id:number, @Req() req) {
		try {
			const r = await this.service.bindDevice(id, req[ReqAttrs.USER_ID])
			return r ? ResponseUtilService.successRes() : ResponseUtilService.failRes()
		} catch (error) {
			return ResponseUtilService.failRes(error.message)
		}
	}

	@Post('unbind')
	async unbind(@Body('id', ParseIntPipe) id:number, @Req() req) {
		try {
			const r = await this.service.unbindDevice(id, req[ReqAttrs.USER_ID])
			return r ? ResponseUtilService.successRes() : ResponseUtilService.failRes()
		} catch (error) {
			return ResponseUtilService.failRes(error.message)
		}
	}

	@Post('start')
	async startDevice(@Body('id', ParseIntPipe) id:number, @Req() req) {
		try {
			const r = await this.service.startDevice(id, req[ReqAttrs.USER_ID], '1')
			return r ? ResponseUtilService.successRes() : ResponseUtilService.failRes()
		} catch (error) {
			return ResponseUtilService.failRes(error.message)
		}
	}

	@Post('stop')
	async stopDevice(@Body('id', ParseIntPipe) id:number, @Req() req) {
		try {
			const r = await this.service.startDevice(id, req[ReqAttrs.USER_ID], '0')
			return r ? ResponseUtilService.successRes() : ResponseUtilService.failRes()
		} catch (error) {
			return ResponseUtilService.failRes(error.message)
		}
	}

	@Get('setRes/:id')
    async setRes(@Param('id') id:string) {
        const res = await this.service.startDeviceRes(id)
        return ResponseUtilService.successRes(res)
    }

	@Post('startClean')
	async startClean(@Body('id', ParseIntPipe) id:number, @Req() req) {
		try {
			const r = await this.service.startClean(id, req[ReqAttrs.USER_ID], '1')
			return r ? ResponseUtilService.successRes() : ResponseUtilService.failRes()
		} catch (error) {
			return ResponseUtilService.failRes(error.message)
		}
	}

	@Post('stopClean')
	async stopClean(@Body('id', ParseIntPipe) id:number, @Req() req) {
		try {
			const r = await this.service.startClean(id, req[ReqAttrs.USER_ID], '0')
			return r ? ResponseUtilService.successRes() : ResponseUtilService.failRes()
		} catch (error) {
			return ResponseUtilService.failRes(error.message)
		}
	}

	@Get('cleanRes/:id')
    async cleanRes(@Param('id') id:string) {
        const res = await this.service.cleanRes(id)
        return ResponseUtilService.successRes(res)
    }
	

	@Post('cleanRecord/page')
    async page(@Body() dto:Pagination, @Req() { userId }) {
        let rows = await this.service.cleanRecordPage(dto, userId)
        return ResponseUtilService.successRes(rows)
    }


	@Post('setMaxFlow')
	async setMaxFlow(@Body('id', ParseIntPipe) id:number, @Req() req) {
		try {
			const r = await this.service.setMaxWaterFlow(id, req[ReqAttrs.USER_ID])
			return r ? ResponseUtilService.successRes() : ResponseUtilService.failRes()
		} catch (error) {
			return ResponseUtilService.failRes(error.message)
		}
	}

	@Get('setMaxFlowRes/:id')
    async setMaxFlowRes(@Param('id') id:string) {
        const res = await this.service.setMaxFlowRes(id)
        return ResponseUtilService.successRes(res)
    }
}
